const {
  cpu,
  currentLoad,
  time,
  // cpuTemperature,
  diskLayout,
  fsSize,
  blockDevices,
  mem,
  memLayout,
  battery,
  inetChecksite,
  networkStats,
  networkInterfaces
} = require('systeminformation')

const minuteSecs = 60
const hourSecs = minuteSecs * minuteSecs
const daySecs = 24 * hourSecs

class SysUtil {
  static defaultChkUrls = ['https://www.baidu.com/', 'https://cn.bing.com/']

  /**
   * 获取cpu所有内核信息
   * returns {avgLoad, currentLoadUser, currentLoadSystem, currentLoadIdle,
   *          cpus[{load,loadIdle,loadSystem,loadUser...}], ...}
   */
  static cpuLoadInfo = async () => {
    return await currentLoad()
  }

  /**
   * 获取cpu信息
   * @returns {Promise<Systeminformation.CpuData>}
   */
  static info = async () => {
    return await cpu()
  }

  /**
   * 系统运行时间
   * @returns {Systeminformation.TimeData}
   */
  static timeInfo = () => {
    const timeInf = time()
    timeInf.runningTime = SysUtil.transferTime(timeInf.uptime)
    // console.log(timeInf)
    return timeInf
  }

  static transferTime = (timeInSecs) => {
    const days = Math.floor(timeInSecs / daySecs)
    let leftSecs = timeInSecs % daySecs
    const hours = Math.floor(leftSecs / hourSecs)
    leftSecs = timeInSecs % hourSecs
    const minutes = Math.floor(leftSecs / minuteSecs)
    const seconds = Math.floor(leftSecs % minuteSecs)
    return {
      days,
      hours: hours < 10 ? '0' + hours : hours,
      minutes: minutes < 10 ? '0' + minutes : minutes,
      seconds: seconds < 10 ? '0' + seconds : seconds
    }
  }

  /**
   * 磁盘信息
   * @returns {Promise<Systeminformation.DiskLayoutData[]>}
   *  {interfaceType: "NVMe", name: "Acer SSD N7000 1TB", size: 1000202273280, type: "SSD", ...}
   */
  static diskInfo = async () => {
    return await diskLayout()
  }

  /**
   * 磁盘空间信息
   * @returns {Promise<Systeminformation.FsSizeData[]>}
   */
  static diskSize = async () => {
    return await fsSize()
  }

  /**
   * 磁盘空间详情
   * @returns {Promise<Systeminformation.BlockDevicesData[]>}
   *  {device, fsType, identifier, label, removable, size, type}
   */
  static diskDetailSize = async () => {
    return await blockDevices()
  }

  /**
   * 内存信息
   * @returns {Promise<Systeminformation.MemData>}
   */
  static memInfo = async () => {
    return await mem()
  }

  /**
   * 内存详情
   * @returns {Promise<Systeminformation.MemLayoutData[]>}
   */
  static memDetail = async () => {
    return await memLayout()
  }

  /**
   * 电池信息
   */
  static batteryInfo = async () => {
    return await battery()
  }

  /**
   * 网络接口
   * @returns {Promise<Systeminformation.NetworkInterfacesData[]|Systeminformation.NetworkInterfacesData>}
   */
  static netInterfaces = async () => {
    return await networkInterfaces()
  }

  /**
   * 网络接口统计
   * @returns {Promise<Systeminformation.NetworkStatsData[]>}
   */
  static netStats = async (ifaces) => {
    return await networkStats(ifaces)
  }

  /**
   * 网络检查
   * @returns {Promise<Systeminformation.InetChecksiteData>}
   */
  static netCheck = async (url) => {
    return await inetChecksite(url)
  }
}

export default SysUtil
